﻿#region

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Radovici.Ctrls.ContentControls;
using Radovici.SharedLibrary;
using YouTubePlaylist.Libraries;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class TrackOptionsCtrl
    {
        public TrackOptionsCtrl()
        {
            InitializeComponent();
        }

        public PlaylistViewModel ViewModel
        {
            get { return DataContext as PlaylistViewModel; }
        }

        private void onRefreshTrack(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoTrack t = ViewModel.PlaylistModel.SelectedTrack;
                if (t != null)
                {
                    ViewModel.PlaylistModel.RefreshTrack(t);
                }
            }
        }

        private void onDeleteTrack(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoTrack t = ViewModel.PlaylistModel.SelectedTrack;
                if (t != null)
                {
                    ViewModel.PlaylistModel.DeleteTrack(t);
                }
            }
        }

        private void OnLink(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoTrack t = ViewModel.PlaylistModel.SelectedTrack;
                if (t != null)
                {
                    Clipboard.SetText(MailClient.GetLink(ViewModel.PlaylistModel.Playlist, t));
                }
            }
        }

        private void OnMail(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoTrack t = ViewModel.PlaylistModel.SelectedTrack;
                if (t != null)
                {
                    var widget = new Widget
                                     {
                                         Content =
                                             new MailLinkInputCtrl(ViewModel.PlaylistModel.Model.User,
                                                                   ViewModel.PlaylistModel.Playlist, t),
                                         Title = "Email a Friend",
                                         OkEnabled = true
                                     };
                    widget.Show(OnMailSent, sender as UIElement);
                }
            }
        }

        private void OnMailSent(object ctrl, EventArgs e)
        {
            var myCtrl = ctrl as MailLinkInputCtrl;
            if (myCtrl != null)
            {
                MailClient.SendMail(myCtrl.From.Text, myCtrl.To.Text, myCtrl.Subject.Text, myCtrl.Body);
            }
        }

        private void OnUrlSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            VideoTrack t = ViewModel.PlaylistModel.SelectedTrack;
            if (t != null)
            {
                if (e.AddedItems != null && e.AddedItems.Count > 0)
                {
                    var url = e.AddedItems[0] as VideoTrackUrl;
                    if (url != null)
                    {
                        t.Url = url;
                        ViewModel.PlaylistModel.SaveTrackUrl(t);
                    }
                }
            }
        }

        private void OnOptionMouseEnter(object sender, MouseEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var option = btn.Tag as string;
                if (option != null)
                {
                    ViewModel.PlaylistModel.SelectedOption = option;
                }
            }
        }

        private void OnOptionMouseLeave(object sender, MouseEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var option = btn.Tag as string;
                if (option != null)
                {
                    if (ViewModel.PlaylistModel.SelectedOption == option)
                    {
                        ViewModel.PlaylistModel.SelectedOption = null;
                    }
                }
            }
        }

        private void OnSelectedTrackUrlsLoaded(object sender, RoutedEventArgs e)
        {
            RefreshSelectedTrackUrls();
        }

        private void RefreshSelectedTrackUrls()
        {
            if (ViewModel == null) return;
            ComboBox cmb = SelectedTrackUrls; // sender as ComboBox;
            if (cmb != null)
            {
                cmb.SelectionChanged -= OnUrlSelectionChanged;
                VideoTrack t = ViewModel.PlaylistModel.SelectedTrack;
                if (t != null)
                {
                    cmb.SelectedItem = t.SelectedUrl;
                }
                cmb.SelectionChanged += OnUrlSelectionChanged;
            }
        }

        private void OnOptionsMouseMove(object sender, MouseEventArgs e)
        {
            ViewModel.IsActive = true;
        }
    }
}